using System;
using System.Web.UI;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace com.flajaxian.AWS
{
    /// <summary>
    /// The tag should be used in the control definition on the aspx page, inside the AccessControlList tag as:
    /// &lt;fjxau:AccessControl URI="http://acs.amazonaws.com/groups/global/AllUsers" Permission="READ" /&gt;
    /// to define the permissions of the uploaded files
    /// </summary>
    [PersistChildren(false), TypeConverter(typeof(ExpandableObjectConverter)), ParseChildren(true)]
    public class AccessControl
    {

        private string _emailAddress;
        private string _id;
        private string _displayName;
        private string _uri;
        private Permission _permission = Permission.READ;
        /// <summary>
        /// 
        /// </summary>
        public AccessControl()
        {
        }

        /// <summary>
        /// Used for "Customer by e-mail grantee"
        /// </summary>
        public string EmailAddress
        {
            get { return this._emailAddress; }
            set { this._emailAddress = value; }
        }

        /// <summary>
        /// Used for "Canonical user grantee"
        /// </summary>
        public string ID
        {
            get { return this._id; }
            set { this._id = value; }
        }

        /// <summary>
        /// Used for "Canonical user grantee
        /// </summary>
        public string DisplayName
        {
            get { return this._displayName; }
            set { this._displayName = value; }
        }

        /// <summary>
        /// Used for "Group grantee" for example to define READ permission for all users use:
        /// &lt;fjxau:AccessControl URI="http://acs.amazonaws.com/groups/global/AllUsers" Permission="READ" /&gt;
        /// </summary>
        public string URI
        {
            get { return this._uri; }
            set { this._uri = value; }
        }

        /// <summary>
        /// Used for "Group grantee" for example to define READ permission for all users use:
        /// &lt;fjxau:AccessControl URI="http://acs.amazonaws.com/groups/global/AllUsers" Permission="READ" /&gt;
        /// Permission can be READ, WRITE, READ_ACP, WRITE_ACP, FULL_CONTROL. Note that attemp to assign FULL_CONTROL
        /// for all the users may result in security error.
        /// </summary>
        public Permission Permission
        {
            get { return this._permission; }
            set { this._permission = value; }
        }
        /// <summary>
        /// Transforms Access control to Grant object
        /// </summary>
        /// <returns>Grant object</returns>
        public Grant ToGrant()
        {
            Grant grant = null;
            if (!String.IsNullOrEmpty(this.EmailAddress))
            {
                grant = new Grant();
                AmazonCustomerByEmailGrantee acbeg = new AmazonCustomerByEmailGrantee();
                acbeg.EmailAddress = this.EmailAddress;
                grant.Grantee = acbeg;
            }
            else if (!String.IsNullOrEmpty(this.ID) && !String.IsNullOrEmpty(this.DisplayName))
            {
                grant = new Grant();
                CanonicalUserGrantee cug = new CanonicalUserGrantee();
                cug.ID = this.ID;
                cug.DisplayName = this.DisplayName;
                grant.Grantee = cug;
            }
            else if (!String.IsNullOrEmpty(this.URI))
            {
                grant = new Grant();
                GroupGrantee gg = new GroupGrantee();
                gg.URI = this.URI;
                grant.Grantee = gg;
            }
            if (grant == null)
            {
                throw new ApplicationException(SR.NotEnoughAccessControlPropertiesWereSet);
            }

            grant.Permission = this.Permission;
            return grant;
        }
    }
}
